import { URL, ajax, Toast, getUser } from '../../utils/index.js'
var innerAudioContext = wx.createInnerAudioContext();
Page({

  data: {
    assessResult:{},
    assessQuestionList:[],
    endimgurl:'../../../../assets/end@2x.png',
    overimgurl:'../../../../assets/finish@2x.png',
    status:'',
    assessId:'',
    audioStatus: false,
    audioId: null,
    head: null
  },
  audioEvent (e) {
    let {voiceKey, questionId} = e.currentTarget.dataset.item
    let {audioId, audioStatus} = this.data
    console.log(questionId != audioId , !audioStatus)
    if (questionId != audioId || !audioStatus) {
      innerAudioContext.destroy()
      innerAudioContext = wx.createInnerAudioContext();
      innerAudioContext.autoplay = true
      innerAudioContext.src = voiceKey
      this.setData({
        audioId: questionId,
        audioStatus: true
      })
    } else {
      innerAudioContext.stop()
      this.setData({
        audioId: null
      })
    }
  },
  setStatus (e) {
    this.setData({
      audioStatus: e
    })
  },
  // 录音监听事件
  audioChange() {
    innerAudioContext.onPlay(() => console.log('开始播放音频事件=============================>'))
    innerAudioContext.onPause(() => (this.setStatus(false), console.log('音频暂停事件=============================>')))
    innerAudioContext.onStop(() => (this.setStatus(false), console.log('音频停止事件=============================>')))
    innerAudioContext.onError((e) => {
      Toast.fail(e.ERRMSG)
      this.setStatus(false), console.log('音频错误事件==========>' + JSON.stringify(e))
    })
    innerAudioContext.onCanplay(() => (console.log('音频进入可以播放状态=============================>')))
    innerAudioContext.onEnded(() => (this.setStatus(false), console.log('音频自然播放至结束事件=============================>')))
  },
  // 全部成绩 按钮 点击事件
  allresult(e){
    wx.navigateTo({ 
      url: `/pages/allResult/allResult?assessId=${e.currentTarget.dataset.assessid}`
    })
  },
  //回顾考核题目 按钮 点击事件
  lookback(){
    wx.navigateTo({ 
      url: `/pages/lookback/lookback?assessId=${this.data.assessId}`
    })
  },
  queryAssessResult(id){
    ajax.loading()
    return new Promise((resolve, reject) => {
        ajax.post('/applet/assess/queryAssessResult',{assessId:id}).then(res => {
            if (res.data.code == 1) {
                resolve(res)
                console.log(res.data.data)
                //res.data.data.assessResult.finishNum=0
                this.setData({
                  assessResult:res.data.data.assessResult,
                  assessQuestionList:res.data.data.assessQuestionList
                })
                ajax.toast.clear()
            } else ajax.toast.fail(res.data.msg), reject(res)
        }).catch(e => (ajax.toast.fail('接口错误onload'), reject(e), console.log(e)))
    })
  },
  onLoad: function (options) {
    this.queryAssessResult(options.assessId - 0 || 4)
    this.setData({
      status: options.status || 1,
      assessId: options.assessId || 4
    })
    this.audioChange()
    getUser().then(res => {
      this.setData({
        head: res.data.data.head
      })
    })
  },

  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})